<?php

/**
 * @file IndexController
 * @author garypradoruiz <garypradoruiz@gmail.com>
 * @link mailto:garypradoruiz@gmail.com
 * @copyleft 2012 garypradoruiz
 * @license GPL
 * @version 1
 */
class Client_IndexController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $client = new Client_Model_DbTable_Client();
        
        $this->view->clients = $client->fetchAll('deleted=0', 'id DESC');
    }

    public function addClientAction() {
        $depdb = new Client_Model_DbTable_Client();
        $request = $this->getRequest();

        if ($request->isPost()) {
            $data = $request->getPost();
            $depdb->insert($data);
            $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Cliente adicionado con exito.');
            $this->_helper->redirector->gotoUrlAndExit('client/index/add-client/');
        }
    }

    public function editClientAction() {
        $id = $this->_getParam('id', 0);
        $client = new Client_Model_DbTable_Client();

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
            $where = $client->getAdapter()->quoteInto('id=?', $id);
            $data['update_date'] = date('Y-m-d H:i:s');
            $client->update($data, $where);
                
            $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Cliente editado con exito.');
            $this->_helper->redirector->gotoUrlAndExit('client/index/edit-client/id/' . $id);
        } else {
            $dataClient = $client->fetchRow('id=' . $id);
            if (!$dataClient) {
                throw new Exception("Fila no encontrada $id");
            } else {
                $this->view->client = $dataClient;
            }
        }
    }

    public function deleteClientAction() {
        $id = $this->_getParam('id', 0);
        $client = new Client_Model_DbTable_Client();
        $where = $client->getAdapter()->quoteInto('id=?', $id);
        $data = array('deleted' => 1 );
        $client->update($data, $where);

        $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Cliente eliminado con exito.');
        $this->_helper->redirector->gotoUrlAndExit('client/');
    }

}

?>
